package bk;

//Adder.java
import java.awt.*; 
import java.awt.event.*;
/* numerical input and output */
public class Adder extends Frame implements ActionListener {
  /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
public static final int WIDTH = 300;
  public static final int HEIGHT = 300;
	public static final int X = 80;
  public static final int Y = 50;

	private TextField inputOutputField; 
  private double sum = 0;
  public static void main(String[] args) {
      Adder guiAdder = new Adder();
      guiAdder.setVisible(true);
  }

  public Adder() {
      setTitle("Adding Machine");
      addWindowListener(new WindowDestroyer());
      setSize(WIDTH, HEIGHT);
      setLocation(X,Y);

      setLayout(new BorderLayout());
      
      Panel buttonPanel = new Panel();
      buttonPanel.setBackground(Color.gray);
      buttonPanel.setLayout(new FlowLayout()); 
      Button addButton = new Button("Add In"); 
      addButton.addActionListener(this);
      buttonPanel.add(addButton); 
      Button resetButton = new Button("Reset"); 
      resetButton.addActionListener(this);
      buttonPanel.add(resetButton);
      add(buttonPanel, "South");
      Panel textPanel = new Panel(); 
   	textPanel.setBackground(Color.blue); 
   	inputOutputField = new TextField(20);
   	inputOutputField.setBackground(Color.white);
  	Label prompt = new Label("Enter a number to add");
  	prompt.setForeground(Color.white);
  	
  	textPanel.add(prompt, "Left");
  	textPanel.add(inputOutputField);
      add(textPanel, "Center");
      }
  public void actionPerformed(ActionEvent e) {
      if (e.getActionCommand().equals("Add In")) {
          sum = sum +
              stringToDouble(inputOutputField.getText());
          inputOutputField.setText(Double.toString(sum));
      }
      else if (e.getActionCommand().equals("Reset")) {
          sum = 0;
          inputOutputField.setText("0.0");
      }
      else 
          inputOutputField.setText("Error in adder code.");
      repaint();
  }
  private static double stringToDouble(String stringObject) {
	  return Double.valueOf(stringObject.trim()).doubleValue();
	  }
	}

